<?php
class PackagePriceController extends CBaseController
{		
	protected $package;
	protected $price;
	protected $validator;
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
		$this->templator->assign('menu','price');
		$this->prepare();
	}
	
	public function prepare()
	{
		if(isset($_GET['id']))
		{
			$id = $_GET['id'];
			$this->package = R::load('travelpackage',$id);
		}
		elseif(isset($_POST['id']))
		{	
			$id = $_POST['id'];
			$this->package = R::load('travelpackage',$id);
		}
		else
		{
			$this->package = R::dispense('travelpackage');
		}
		$this->preparePrice();
	}	
	
	public function preparePrice()
	{
		if(isset($_GET['price_id']))
		{
			$id = $_GET['price_id'];
			$this->price = R::load('price',$id);
		}
		elseif(isset($_POST['price_id']))
		{	
			$id = $_POST['price_id'];
			$this->price = R::load('price',$id);
		}
		else
		{
			$this->price = R::dispense('price');
		}
	}
	
	public function saveORupdateValidate()
	{
		$this->validator = new FormValidator();
		$this->validator->addValidation("name","req","Please fill in Name");
		return $this->validator->ValidateForm();
	}
	
	public function savePrepare()
	{
		$this->price->name = $_POST['name'];
		$this->price->position = $_POST['position'];
		$this->price->currency = $_POST['currency'];
		$this->price->orginalprice = $_POST['orginalprice'];
		$this->price->finalprice = $_POST['finalprice'];
		$this->price->unit = $_POST['unit'];
		$this->price->quantity = $_POST['quantity'];
		$this->price->type = $_POST['type'];
		$this->price->desc = $_POST['desc'];
		$this->price->pckgOption_id = $_POST['pckgOption'];
	}
		
	
	public function save()
	{
		$this->savePrepare();
		  if($this->saveORupdateValidate())
		  {
			if(isset($this->price->id) && !empty($this->price->id))
				$this->package->ownPrice[$this->price->id]=$this->price;
			else
				$this->package->ownPrice[]=$this->price;
			R::store($this->package);
			$this->templator->assign('info','price added sucessfully');
			$this->price = R::dispense('price');
			$this->prices();	
		  }
		  else
		  {
			$error_hash = $this->validator->GetErrors();
			$this->templator->assign('validate_errors',$error_hash);
			$this->prices();
		  }
			
	}
	
	public function update()
	{
		$this->save();	
	}
	
	public function edit()
	{
		$this->prices();	
	}
	
	public function delete()
	{
		R::trash($this->price);
		$this->price = R::dispense('price');
		$this->prices();	
	}
	
	public function prices()
	{		
		//$assignedPrice= R::related( $this->package, 'price' );
		$this->templator->assignByRef('package',$this->package);		
		$this->templator->assign('pckgOptions',array_merge($this->package->ownPckgOption, array()));		
		$this->templator->assignByRef('price',$this->price);		
		$this->templator->assign('prices',array_merge($this->package->ownPrice, array()));
		$this->templator->display('price/package_price.html');
	}
	
	
}	